﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Domain.FinancialReport;

namespace S4Store.Service
{
    public class FinancialReportService : BaseService
    {
        public FinancialCommonReport GetStoreReport(int year, int month, int storeid)
        {
            FinancialKpiService FinancialKpiService = new Service.FinancialKpiService();
            this.GetTranstion().Inject(FinancialKpiService);
            FinancialCommonReport report = new FinancialCommonReport();
            report.FinancialMonthConfig = FinancialKpiService.GetFinancialMonthConfig(year, month, storeid, true);
            report.FinancialCarBrandMonthConfigs = FinancialKpiService.GetFinancialCarBrandMonthConfig(year, month, storeid, true).ToList();
            report.FinancialDayDatas = FinancialKpiService.GetFinancialDayDatas(year, month, storeid, true).ToList();
            DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
            report.Year = dt.Year;
            report.Month = dt.Month;
            report.Day = dt.Day;
            return report;
        }

        public CarBrandFinancialReport GetCarBrandReport(int year, int month, int storeid,int carbrandid)
        {
            FinancialKpiService FinancialKpiService = new Service.FinancialKpiService();
            CarService CarService = new Service.CarService();
            this.GetTranstion().Inject(FinancialKpiService);
            this.GetTranstion().Inject(CarService);
            CarBrandFinancialReport report = new CarBrandFinancialReport();
            report.FinancialMonthConfig = FinancialKpiService.GetFinancialMonthConfig(year, month, storeid, true);
            report.FinancialCarBrandMonthConfig = FinancialKpiService.GetFinancialCarBrandMonthConfig(year, month, storeid, true).FirstOrDefault(t=>t.CarBrandId==carbrandid);
            if (report.FinancialCarBrandMonthConfig == null)
            {
                report.FinancialCarBrandMonthConfig = new Domain.FinancialKpi.FinancialCarBrandMonthConfig()
                {
                    Month = new DateTime(year, month, 1).MonthLastOrNow(),
                    CarBrandId = carbrandid,
                    StoreId = storeid
                };
            }
            report.FinancialDayDatas = FinancialKpiService.GetFinancialDayDatas(year, month, storeid, true).Where(t=>t.CarBrandId==carbrandid).ToList();
            report.StoreReport = new FinancialCommonReport();
            report.StoreReport.FinancialDayDatas = FinancialKpiService.GetFinancialDayDatas(year, month, storeid, true).ToList();
            DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
            report.Year = dt.Year;
            report.Month = dt.Month;
            report.Day = dt.Day;
            report.CarBrand = CarService.GetCarBrandQuery().FirstOrDefault(t => t.Id == carbrandid);
            return report;

        }
    }
}
